package cn.ssm.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import cn.ssm.pojo.Student;
import cn.ssm.service.StudentService;



@Controller
@RequestMapping("/studentController")
public class StudentController {
	@Autowired
	@Qualifier("studentServiceImpl")
	private StudentService studentService;
	 
	
	
	
	@RequestMapping("/selectStudent")
	public ModelAndView selectStudent(@ModelAttribute Student student){
		ModelAndView mav = new ModelAndView();
			List<Student> sList = studentService.findStudentAll();
			if(sList!=null){
				mav.addObject("slist", sList);
				mav.setViewName("studentList");	
			}
			else {
				mav.setViewName("login");
			}
		return mav;
	}
	
	
	
	@RequestMapping("/addStudent")
	public ModelAndView addStudent(@ModelAttribute Student student){
		ModelAndView mav = new ModelAndView();
		int num = studentService.addStudent(student);
		if(num > 0){
			List<Student> sList = studentService.findStudentAll();
			mav.addObject("slist", sList);
			mav.setViewName("studentList");
		}else{
			mav.setViewName("addStudent");
		}
		return mav;
	}
	
	
	
	@RequestMapping("/delStudent")
	public ModelAndView delStudent(Integer studentNo/*Map<String,Object> modelModel model*/){
		ModelAndView mav = new ModelAndView();
		int num=studentService.delStudentByNo(studentNo);
		if(num>0){
			List<Student> slist=studentService.findStudentAll();
			mav.addObject("slist",slist);
			mav.setViewName("studentList");
		}else{
			mav.setViewName("studentList");
		}
		return mav;
	}
	
	
	
	@RequestMapping("/updateStudent")
	public ModelAndView updateStudenat( Student student){
		ModelAndView mav = new ModelAndView();
		int num = studentService.updateStudent(student);
		if(num>0){
			List<Student> sList = studentService.findStudentAll();
			mav.addObject("slist", sList);
			mav.setViewName("studentList");
		}else{
			mav.setViewName("studentList");
		}
		return mav;
	}
}
